package chapter6.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import chapter6.entity.Customer;

@Controller("/ch62")
public class CustomerController_ch62 {
    //返回ModelAndView类型
    @RequestMapping(value="/userList")
    protected ModelAndView userList(){
        List<Customer> customers = new ArrayList<Customer>();
        Customer c1 = new Customer();
        c1.setCustId(1);
        c1.setName("张三");
        c1.setAge(22);
        customers.add(c1);
        Customer c2 = new Customer();
        c2.setCustId(2);
        c2.setName("李四");
        c2.setAge(20);
        customers.add(c2);
        Map<String,Object> model = new HashMap<String,Object>();
        model.put("customers", customers);
        ModelAndView modelAndView = new ModelAndView("customerList",model);
        return modelAndView;
    }

    //返回String类型
    @GetMapping(value="/updateView")
    protected String updateView(){
        return "customerEdit";
    }

    //返回String类型（使用Model返回页面需要的值）
    @RequestMapping(value="/updateUserView")
    protected String updateUserView(Model model){
        Customer c1 = new Customer();
        c1.setCustId(1);
        c1.setName("张三");
        c1.setAge(22);
        model.addAttribute("customers", c1);
        return "customerEdit";
    }

    //返回String类型（重定向与请求转发）
    @RequestMapping(value="/update")
    protected String update(String name,int age){
        List<Customer> customers = new ArrayList<Customer>();

        Customer c1 = new Customer();
        c1.setCustId(1);
        c1.setName(name);
        c1.setAge(age);
        customers.add(c1);

        return "redirect:userList";
        //return "forward:userList";
    }

    //返回ModelAndView类型(使用视图解析器)
    @RequestMapping(value="/userList2")
    protected ModelAndView userList2(){
        List<Customer> customers = new ArrayList<Customer>();

        Customer c1 = new Customer();
        c1.setCustId(1);
        c1.setName("张三");
        c1.setAge(22);
        customers.add(c1);

        Customer c2 = new Customer();
        c2.setCustId(2);
        c2.setName("李四");
        c2.setAge(20);
        customers.add(c2);

        Map<String,Object> model = new HashMap<String,Object>();
        model.put("customers", customers);

        ModelAndView modelAndView = new ModelAndView("customerList",model);
        return modelAndView;
    }

    //返回String类型(使用视图解析器)
    @RequestMapping(value="/userList3")
    protected String userList3(Model model){
        List<Customer> customers = new ArrayList<Customer>();
        Customer c1 = new Customer();
        c1.setCustId(1);
        c1.setName("张三");
        c1.setAge(22);
        customers.add(c1);
        Customer c2 = new Customer();
        c2.setCustId(2);
        c2.setName("李四");
        c2.setAge(20);
        customers.add(c2);
        model.addAttribute("customers", customers);
        return "customerList";
    }
}
